package grasshopper.gcm.db;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.eclipse.persistence.annotations.ConversionValue;
import org.eclipse.persistence.annotations.Convert;
import org.eclipse.persistence.annotations.ObjectTypeConverter;

/**
 *
 *
 * @author piotr.kobik@gmail.com
 */
@Entity
@Table(name = "COADDRESS_CA")
public class CompanyAddress implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CAID")
    private Long id;
    @ManyToOne
    @JoinColumn(name = "COCOMPANYID", nullable = false)
    private Company company;
    @Column(name = "CATYPE",
            columnDefinition = "char(2) not null")
    //TODO add check in constraint for ('LE', 'DE', 'SH', 'IN', 'PA', 'PO')
    @ObjectTypeConverter(name = "addressType", dataType = String.class,
    objectType = CompanyAddress.AddressType.class,
    conversionValues = {
        @ConversionValue(dataValue = "LE", objectValue = "Legal"),
        @ConversionValue(dataValue = "DE", objectValue = "Delivery"),
        @ConversionValue(dataValue = "SH", objectValue = "Shipping"),
        @ConversionValue(dataValue = "IN", objectValue = "Invoice"),
        @ConversionValue(dataValue = "PA", objectValue = "Payer"),
        @ConversionValue(dataValue = "PO", objectValue = "Postal")
    })
    @Convert(value = "addressType")
    private AddressType type;
    @Column(name = "CANAME")
    private String name;
    @Column(name = "CAADDR1")
    private String address1;
    @Column(name = "CAADDR2")
    private String address2;
    @Column(name = "CAZIP")
    private String zip;
    @Column(name = "CACITY")
    private String city;
    @Column(name = "CASTATE")
    private String state;
    @Column(name = "CACOUNTRY")
    private String country;
    @Column(name = "CALANG")
    private String language;
    @Column(name = "CAPERSON")
    private String contactPerson;
    @Column(name = "CAPHONE")
    private String phone;
    @Column(name = "CAFAX")
    private String fax;
    @Column(name = "CAEMAIL")
    private String email;

    public CompanyAddress() {
    }

    public CompanyAddress(Company c, AddressType type) {
        this.company = c;
        this.type = type;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company c) {
        this.company = c;
    }

    public AddressType getType() {
        return type;
    }

    public void setType(AddressType type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress1() {
        return address1;
    }

    public void setAddress1(String address1) {
        this.address1 = address1;
    }

    public String getAddress2() {
        return address2;
    }

    public void setAddress2(String address2) {
        this.address2 = address2;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getContactPerson() {
        return contactPerson;
    }

    public void setContactPerson(String contactPerson) {
        this.contactPerson = contactPerson;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getFax() {
        return fax;

    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    //--------------------------------------------------------------------------
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CompanyAddress other = (CompanyAddress) obj;
        if (this.company != other.company
            && (this.company == null
            || !this.company.equals(other.company))) {
            return false;
        }
        if ((this.type == null) ? (other.type != null) : !this.type.equals(other.type)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "grasshopper.gcm.db.CompanyAddress[id=" + id + "]";
    }

    public enum AddressType {

        Legal, Delivery, Shipping, Invoice, Payer, Postal;
    }
}
